library(data.table)
library(ggplot2)
library(xtable)
library(corrplot)

data <- data.table(read.csv("group-level_fixed.csv", stringsAsFactors = FALSE))

## Number of side switches:
data[, sideswitch := ifelse(deliberate!=0 | contextual!=0, 1, 0)]
data[, sideswitch2 := length(deliberate)>1,by=gaardid]
table(data$sideswitch2)
table(data$entry_type,data$sideswitch2)
names(data)
no.groups<- c(length(unique(data[ data$sideswitch2==F,]$gaardid)),
              length(unique(data[ data$sideswitch2==T,]$gaardid)),
              length(unique(data$gaardid)))
no.alig <- c(0,sum(data$sideswitch2==FALSE),sum(data$sideswitch2==TRUE))
no.ag <- c(data[, sum(entry_type==0 & sideswitch2==F)],
           data[, sum(entry_type==0 & sideswitch2==T)],
           data[, sum(entry_type==0)])

no.pg <- c(data[, sum(entry_type==1 & sideswitch2==F)],
           data[, sum(entry_type==1 & sideswitch2==T)],
           data[, sum(entry_type==1)])

no.pug <- c(data[, sum(entry_type==2 & sideswitch2==F)],
           data[, sum(entry_type==2 & sideswitch2==T)],
           data[, sum(entry_type==2)])

tab.group <- rbind(no.groups,no.alig,no.ag,no.pg,no.pug)


## Table 1: alignment type
gt <- as.data.frame(tab.group[3:5,3])
names(gt) <-"Frequency"
t <- sum(gt$Frequency)
gt<-rbind(gt,t)
row.names(gt) <-c("Anti-government", "Pro-government", "Unaligned", "Total")
gt$Percentage <- NA
gt$Percentage[1] <- gt[1,1]/gt[4,1]
gt$Percentage[2] <- gt[2,1]/gt[4,1]
gt$Percentage[3] <- gt[3,1]/gt[4,1]
gt$Percentage[4] <- gt[4,1]/gt[4,1]
gt$Percentage <- paste(round(gt$Percentage * 100, digits=1), "%", sep="")

print(xtable(gt, caption="Number of alignments by type (1989-2007)",label="sum3",digit=0),
      include.rownames=T,
      hline.after = c(-1,0,3,4),
      file="Table1-haadgt.tex")


#####Table 2: unique groups with and without alignment change
ng <- as.data.frame(tab.group[1,])
names(ng) <- "Frequency"
Groups <-as.data.frame(c( "Without alignment change","With alignment change", "Total"))
names(Groups) <- "Groups"

Groups <- cbind(Groups,ng)
Groups$Percentage <- NA
Groups$Percentage[1] <- (Groups$Frequency[1] / Groups$Frequency[3] ) * 100
Groups$Percentage[2] <- (Groups$Frequency[2] / Groups$Frequency[3] ) * 100
Groups$Percentage[3] <- (Groups$Frequency[3] / Groups$Frequency[3] ) * 100
Groups$Percentage <- round(Groups$Percentage,1)
Groups$Percentage <- paste(Groups$Percentage, "%", sep="")

print(xtable(Groups,caption="Frequency of alignment changes (1989-2007)", 
             label="sum1", digits=0),
      include.rownames=F,
      hline.after = c(-1,0,2,3),
      file="able2-groups1.tex")



### Transition matrix for alignment changes
data$entry_align <- "Anti-government"
data$entry_align <- ifelse(data$entry_type == 1, "Pro-government", data$entry_align)
data$entry_align <- ifelse(data$entry_type == 2, "Unaligned", data$entry_align)

data$exit_align <- "Anti-government"
data$exit_align <- ifelse(data$exit_type == 1, "Pro-government", data$exit_align)
data$exit_align <- ifelse(data$exit_type == 2, "Unaligned", data$exit_align)

transition <- data[entry_align != exit_align, list(entry_align, exit_align)]
S <- as.data.frame(table(transition$entry_align, transition$exit_align))
S$Freq <- ifelse(S$Freq==0, NA, S$Freq)
S$Var1 <- paste("From:\n", S$Var1, sep="")
S$Var2 <- paste("To:\n", S$Var2, sep="")

M <- matrix(S$Freq, nrow = 3, ncol = 3,
       dimnames = list(unique(S$Var1), unique(S$Var2)))
M <- round(M/sum(M, na.rm=T), digits=2)
pdf(file = "Figure3-direction-alignment-changes.pdf", width=4, height=4)
corrplot(M, method="color", type = "full", is.corr = FALSE,
         addCoef.col = "white",
         addrect = 2, 
         col = grey.colors(100, start = 0, end = 1,rev = TRUE),
         cl.pos = "n",
         na.label = "X",
         tl.col = "black",
         addgrid.col = "black")
dev.off()
